//
//  BodyIngredientCell.m
//  SHHCPhone
//
//  Created by sheng long on 14-6-30.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "BodyIngredientCell.h"

@interface BodyIngredientCell()
{
    UIImageView *bgImage;
    
    UIImageView *headImage;
    UILabel *titleLabel;
    UIImageView *flagImage;
    UILabel *flagLabel;
    
    UIView *lineView;
    CGFloat height;
}
@end

@implementation BodyIngredientCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier size:(CGSize)size
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        height = size.height;
        
        lineView = [[UIView alloc] initWithFrame:CGRectZero];
        [lineView setBackgroundColor:GET_COLOR(@"f6f6f6")];
        [self.contentView addSubview:lineView];
        
        bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"体成分-背景.png"]];
        [bgImage sizeToFit];
        [bgImage setCenter:CGPointMake(size.width-24-bgImage.frame.size.width/2, size.height/2)];
        [self.contentView addSubview:bgImage];
        
        headImage = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:headImage];
        
        titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [titleLabel setBackgroundColor:[UIColor clearColor]];
        [titleLabel setFont:[UIFont systemFontOfSize:13]];
        [titleLabel setTextColor:GET_COLOR(@"868686")];
        [self.contentView addSubview:titleLabel];
        
        flagImage = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:flagImage];
        
        flagLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [flagLabel setBackgroundColor:[UIColor clearColor]];
        [flagLabel setFont:[UIFont systemFontOfSize:12]];
        [flagLabel setTextColor:GET_COLOR(@"fcab86")];
        [self.contentView addSubview:flagLabel];
    }
    return self;
}

-(void)setContent:(BodyIngredientInfo*)info
{
    [headImage setImage:[UIImage imageNamed:info.headImageName]];
    [headImage sizeToFit];
    [headImage setCenter:CGPointMake(bgImage.frame.origin.x/2, bgImage.center.y)];
    
    CGRect rect;
    CGFloat width = 2;
    switch (info.Loc) {
        case 0:
        {
            //中间位置
            rect = CGRectMake(headImage.center.x-width/2, 0, width, height);
        }
            break;
        case 1:
        {
            //第一个
            rect = CGRectMake(headImage.center.x-width/2, headImage.frame.size.height+headImage.frame.origin.y, width, height-(headImage.frame.size.height+headImage.frame.origin.y));
        }
            break;
        case 2:
        {
            //最后一个
            rect = CGRectMake(headImage.center.x-width/2, 0, width, headImage.frame.origin.y);
        }
            break;
        case 3:
        {
            //没有线
            rect = CGRectZero;
        }
            break;
        default:
            break;
    }
    [lineView setFrame:rect];
    
    [titleLabel setText:info.title];
    [titleLabel sizeToFit];
    [titleLabel setCenter:CGPointMake(bgImage.frame.origin.x+15+titleLabel.frame.size.width/2, bgImage.center.y)];
    
    [flagLabel setText:info.flagText];
    [flagLabel sizeToFit];
    [flagLabel setCenter:CGPointMake(bgImage.frame.origin.x+bgImage.frame.size.width-10-flagLabel.frame.size.width/2, bgImage.center.y)];
    
    NSString *flagName = nil;
    switch (info.type) {
        case BODYI_TYPE_HI:
        {
            flagName = @"体成分-超标icon.png";
        }
            break;
        case BODYI_TYPE_BZ:
        {
            flagName = @"体成分-标注icon.png";
        }
            break;
        case BODYI_TYPE_LOW:
        {
            flagName = @"体成分-偏低icon.png";
        }
            break;
        default:
            flagName = nil;
            break;
    }
    [flagImage setImage:[UIImage imageNamed:flagName]];
    [flagImage sizeToFit];
    [flagImage setCenter:CGPointMake(flagLabel.frame.origin.x-flagImage.frame.size.width/2-5, bgImage.center.y)];
}

@end
